﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SAC.Elink;
using SAC.sqlHelper;
using System.Collections.Generic;
using ZedGraph;
using ZedGraph.Web;
using System.Drawing;
using System.Drawing.Text;


public partial class BigScreen : System.Web.UI.Page
{
    public string dtNow = "";
    public string dtss;
    public string show = "";

    public double gl1 = 0;
    public double gl2 = 0;
    public double gl0 = 0;

    public double fdl1 = 0;
    public double fdl2 = 0;
    public double fdl0 = 0;

    public double yfdl1 = 0;
    public double yfdl2 = 0;
    public double yfdl0 = 0;

    public double nfdl1 = 0;
    public double nfdl2 = 0;
    public double nfdl0 = 0;

    public string ycw1;
    public string ycw2;
    public string ycw0;

    public string ncw1;
    public string ncw2;

    public string ncw0;

    public string ywc1;
    public string ywc2;
    public string ywc0;

    public string nwc1;
    public string nwc2;
    public string nwc0;

    public string ydjh;
    public string ndjh;

    //曲线
    double[] value = new double[26];
    double[] value2 = new double[26];
    string[] dtTime = new string[26];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Elink ek = new Elink();

            object obj;
            string bm = "";
            string sql = "";
            string qsrq = "";
            string jsrq = "";
            string errMsg = "";

            string pID1 = "fdj1zxyg";               //1号机组电量编号
            string pID2 = "fdj2zxyg";               //2号机组电量编号

            string xl1ID1 = "zb1zxyg";              //天瑞2836线主表正向有功 xl1zxyg              
            string xl2ID1 = "zb2zxyg";              //天瑞2836线主表正向有功 xl1zxyg 

            //有功功率
            string pgl1 = "WHSIS.U1APSH.U1A04013";
            string pgl2 = "WHSIS.U2APSH.U2A04013";

            gl1 = ek.GetRTValue(pgl1);
            gl2 = ek.GetRTValue(pgl2);

            gl0 = gl1 + gl2;

            DateTime dt = DateTime.Now.AddDays(-1);

            dtNow = dt.ToString("yyyy-MM-dd");

            dtss = DateTime.Now.ToString("yyyy-MM-dd");

            //日值
            qsrq = dt.ToString("yyyy-MM-dd 0:00:00");
            jsrq = dt.AddDays(+1).ToString("yyyy-MM-dd 00:00:00");

            SAC.Power.PowerCale pc = new SAC.Power.PowerCale();

            fdl1 = double.Parse(pc.GetPower(fdl1, pID1, qsrq, jsrq, bm).ToString()) / 10000;
            fdl2 = double.Parse(pc.GetPower(fdl2, pID2, qsrq, jsrq, bm).ToString()) / 10000;

            fdl0 = fdl2 + fdl1;

            //月累计
            qsrq = dt.ToString("yyyy-MM-01 0:00:00");
            jsrq = dt.AddDays(+1).ToString("yyyy-MM-dd 00:00:00");

            yfdl1 = double.Parse(pc.GetPower(yfdl1, pID1, qsrq, jsrq, bm).ToString()) / 10000;
            yfdl2 = double.Parse(pc.GetPower(yfdl2, pID2, qsrq, jsrq, bm).ToString()) / 10000;

            yfdl0 = yfdl2 + yfdl1;

            double daysM = DateTime.DaysInMonth(dt.Year, dt.Month);
            double dayM = dt.Day;

            //月度计划值
            sql = "SELECT 计划值 FROM 月度电量计划表 where 日期='" + qsrq + "' and 机组=1";

            obj = DBsql.RunSingle(sql, out errMsg);

            if (obj != null && obj.ToString() != "0")
            {
                if (obj != null && obj.ToString() != "")
                {
                    double wc1 = yfdl1 / double.Parse(obj.ToString()) * 100;
                    ywc1 = wc1.ToString("f2");
                    double cw1 = wc1 - dayM / daysM * 100;

                    if (cw1 < 0)
                        ycw1 = "<font color=\"#FF0000\">" + cw1.ToString("f2") + "</font> ";
                    else
                        ycw1 = cw1.ToString("f2");
                }
                else
                {
                    ywc1 = "&nbsp;";
                    ycw1 = "&nbsp;";
                }
            }
            else
            {
                ywc1 = "/";
                ycw1 = "/";
            }
            sql = "SELECT 计划值 FROM 月度电量计划表 where 日期='" + qsrq + "' and 机组=2";

            obj = DBsql.RunSingle(sql, out errMsg);

            if (obj != null && obj.ToString() != "0")
            {
                if (obj != null && obj.ToString() != "")
                {
                    double wc2 = yfdl2 / double.Parse(obj.ToString()) * 100;
                    ywc2 = wc2.ToString("f2");
                    double cw2 = wc2 - dayM / daysM * 100;


                    if (cw2 < 0)
                        ycw2 = "<font color=\"#FF0000\">" + cw2.ToString("f2") + "</font> ";
                    else
                        ycw2 = cw2.ToString("f2");


                }
                else
                {
                    ywc2 = "&nbsp;";
                    ycw2 = "&nbsp;";
                }
            }
            else
            {
                ywc2 = "/";
                ycw2 = "/";
            }
            sql = "SELECT 计划值 FROM 月度电量计划表 where 日期='" + qsrq + "' and 机组=0";

            obj = DBsql.RunSingle(sql, out errMsg);

            if (obj != null && obj.ToString() != "0")
            {
                if (obj != null && obj.ToString() != "")
                {
                    ydjh = obj.ToString();
                    double wc3 = yfdl0 / double.Parse(obj.ToString()) * 100;
                    ywc0 = wc3.ToString("f2");
                    double cw3 = wc3 - dayM / daysM * 100;

                    if (cw3 < 0)
                        ycw0 = "<font color=\"#FF0000\">" + cw3.ToString("f2") + "</font> ";
                    else
                        ycw0 = cw3.ToString("f2");

                }
                else
                {
                    ydjh = "&nbsp;";
                    ywc0 = "&nbsp;";
                    ycw0 = "&nbsp;";
                }
            }
            else
            {
                ywc0 = "/";
                ycw0 = "/";
            }

            //年累计
            qsrq = dt.ToString("yyyy-01-01 0:00:00");
            jsrq = dt.AddDays(+1).ToString("yyyy-MM-dd 00:00:00");

            nfdl1 = double.Parse(pc.GetPower(nfdl1, pID1, qsrq, jsrq, bm).ToString()) / 10000;
            nfdl2 = double.Parse(pc.GetPower(nfdl2, pID2, qsrq, jsrq, bm).ToString()) / 10000;

            nfdl0 = nfdl2 + nfdl1;

            double daysY = this.retDaysInYear(dt);
            double dayY = this.retDayYear(dt);

            //年度计划值
            sql = "SELECT 计划值 FROM 年度电量计划表 where 日期='" + qsrq + "' and 机组=1";

            obj = DBsql.RunSingle(sql, out errMsg);

            if (obj != null && obj.ToString() != "0")
            {
                if (obj != null && obj.ToString() != "")
                {
                    double wc1 = nfdl1 / double.Parse(obj.ToString()) * 100;
                    nwc1 = wc1.ToString("f2");

                    double cw1 = wc1 - dayY / daysY * 100;

                    if (cw1 < 0)
                        ncw1 = "<font color=\"#FF0000\">" + cw1.ToString("f2") + "</font> ";
                    else
                        ncw1 = cw1.ToString("f2");
                }
                else
                {
                    ncw1 = "&nbsp;";
                    nwc1 = "&nbsp;";
                }
            }
            else
            {

                ncw1 = "/";
                nwc1 = "/";
            }
            sql = "SELECT 计划值 FROM 年度电量计划表 where 日期='" + qsrq + "' and 机组=2";

            obj = DBsql.RunSingle(sql, out errMsg);

            if (obj != null && obj.ToString() != "0")
            {
                if (obj != null && obj.ToString() != "")
                {
                    double wc2 = nfdl2 / double.Parse(obj.ToString()) * 100;
                    nwc2 = wc2.ToString("f2");

                    double cw2 = wc2 - dayY / daysY * 100;

                    if (cw2 < 0)
                        ncw2 = "<font color=\"#FF0000\">" + cw2.ToString("f2") + "</font> ";
                    else
                        ncw2 = cw2.ToString("f2");

                }
                else
                {
                    nwc2 = "&nbsp;";
                    ncw2 = "&nbsp;";
                }
            }
            else
            {
                nwc2 = "/";
                ncw2 = "/";

            }
            sql = "SELECT 计划值 FROM 年度电量计划表 where 日期='" + qsrq + "' and 机组=0";

            obj = DBsql.RunSingle(sql, out errMsg);

            if (obj != null && obj.ToString() != "0")
            {
                if (obj != null && obj.ToString() != "")
                {
                    ndjh = obj.ToString();
                    double wc3 = nfdl0 / double.Parse(obj.ToString()) * 100;
                    nwc0 = wc3.ToString("f2");

                    double cw3 = wc3 - dayY / daysY * 100;

                    if (cw3 < 0)
                        ncw0 = "<font color=\"#FF0000\">" + cw3.ToString("f2") + "</font> ";
                    else
                        ncw0 = cw3.ToString("f2");
                }
                else
                {
                    ndjh = "&nbsp;";
                    nwc0 = "&nbsp;";
                    ncw0 = "&nbsp;";
                }
            }
            else
            {
                nwc0 = "/";
                ncw0 = "/";
            }

            //安全天数
            string aqts = "";

            sql = " SELECT 开始日期 FROM 安全天数事件表";

            obj = DBsql.RunSingle(sql, out errMsg);

            if (obj != null && obj.ToString() != "")
            {
                TimeSpan ts = DateTime.Now.AddDays(-1) - DateTime.Parse(obj.ToString());
                aqts = ts.Days.ToString();
            }
            else
                aqts = "&nbsp;";

            show = DateTime.Now.AddDays(-1).ToShortDateString() + "连续安全运行" + aqts + "天，月度电量计划 " + ydjh + "万Kwh，年度电量计划 " + ndjh + "万Kwh";
            int t = 0;
            int k = 0;
            int ret = 0;
            int ret2 = 0;
            double val = 0;
            double val2 = 0;
            string sDate = "";


            for (int i = 0; i < 26; i++)
            {
                if (i < 24)
                {
                    sDate = DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 0:00:00")).AddHours(i).ToString("yyyy-MM-dd H:00:00");
                }
                else
                {
                    sDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 0:00:00")).AddHours(t).ToString("yyyy-MM-dd H:00:00");
                    t++;
                }

                k = i;

                dtTime[i] = k.ToString();

                ek.GetHisValue("WHSIS.U1APSH.U1A00023", sDate, ref ret, ref val);

                value[i] = val;

                ek.GetHisValue("WHSIS.U2APSH.U2A00023", sDate, ref ret2, ref val2);

                value2[i] = val2;
            }
        }
    }

    /// <summary>
    /// 画图事件
    /// </summary>
    /// <param name="webObject"></param>
    /// <param name="g"></param>
    /// <param name="pane"></param>
    protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        //sResponse.Write(this.Date1.Value);

        GraphPane myPane = pane[0];

        // Set the title and axis labels

        myPane.TitleGap = 0;
        myPane.Margin.Left = 0;
        myPane.Margin.Right = 0;
        myPane.Margin.Bottom = 1.5f;

        //myPane.XAxis.Title.Gap = 0;
        //myPane.XAxis.Title.Text = "(h)";
        //myPane.XAxis.Title.FontSpec.FontColor = Color.Red;
        //myPane.XAxis.Title.FontSpec.Size = 24;

        myPane.YAxis.Title.Text = "(MW)";
        myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
        myPane.YAxis.Title.FontSpec.Size = 24;

        myPane.Legend.FontSpec.Size = 22;
        myPane.Legend.Fill = new Fill(Color.Black);
        myPane.Legend.FontSpec.Fill = new Fill(Color.Black);
        myPane.Legend.FontSpec.FontColor = Color.Red;

        PointPairList list1 = new PointPairList();
        PointPairList list2 = new PointPairList();

        int k = 0;
        for (int i = 0; i < dtTime.Length; i++)
        {
            k = i;
            if (value[i] != null)
                list1.Add(k, double.Parse(value[i].ToString()));
            else
                list1.Add(k, 0);

            if (value2[i] != null)
                list2.Add(k, double.Parse(value2[i].ToString()));
            else
                list2.Add(k, 0);

        }

        //Generate a red curve with diamond
        //symbols, and "My Curve" in the legend
        LineItem myCurve3 = myPane.AddCurve("昨日负荷",
      null, Color.Black, SymbolType.Diamond);

        LineItem myCurve = myPane.AddCurve("#1号机",
             list1, Color.Orange, SymbolType.Diamond);

        LineItem myCurve2 = myPane.AddCurve("#2号机",
          list2, Color.Blue, SymbolType.Diamond);

        //Make the curve smooth
        myCurve.Line.IsSmooth = true;
        myCurve2.Line.IsSmooth = true;

        //Set the XAxis to Text type
        //myPane.XAxis.Type = AxisType.LinearAsOrdinal;
        // Set the XAxis labels
        myPane.XAxis.Scale.TextLabels = dtTime;// labels;
        //Set the labels at an angle so they don't overlap
        //myPane.XAxis.Scale.FontSpec.Angle = 0;

        ///Set the XAxis to Text type

        myPane.XAxis.Type = AxisType.Text; //AxisType.Text
        // myPane.XAxis.CrossAuto = true;
        myPane.XAxis.Scale.Min = 1;
        myPane.XAxis.Scale.Max = 26;
        myPane.XAxis.Scale.BaseTic = 1;



        // Set the XAxis labels
        //myPane.XAxis.Scale.TextLabels = lable;
        // Set the labels at an angle so they don't overlap
        //myPane.XAxis.ScaleFontSpec.Angle = 40;
        //myPane.XAxis.Scale.FontSpec.Angle = 45;

        myPane.XAxis.Scale.FontSpec.Size = 24;
        myPane.XAxis.Color = Color.Red;
        myPane.XAxis.MinorTic.Color = Color.Red;
        myPane.XAxis.Scale.FontSpec.FontColor = Color.Red;

        myPane.YAxis.Color = Color.Red;
        myPane.YAxis.Scale.FontSpec.Size = 24;
        myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;

        //myPane.Chart.Fill.Color = Color.Black;
        myPane.Chart.Fill = new Fill(Color.Black, Color.Black, Color.Black, 45.0F);
        myPane.Chart.Border.Color = Color.Red;

        //// title 设置

        //填充背景颜色
        // Fill the axis background with a color gradient
        myPane.Fill = new Fill(Color.Black, Color.Black, Color.Black, 45.0F);//Color.LightGoldenrodYellow

        // 在数据变化时绘制图形
        myPane.AxisChange(g);
    }


    ///// <summary>
    ///// 画图事件
    ///// </summary>
    ///// <param name="webObject"></param>
    ///// <param name="g"></param>
    ///// <param name="pane"></param>
    //protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    //{
    //    //sResponse.Write(this.Date1.Value);

    //    GraphPane myPane = pane[0];

    //    // Set the title and axis labels

    //    myPane.TitleGap = 0;
    //    myPane.Margin.Left = 0;
    //    myPane.Margin.Right = 0;
    //    myPane.Margin.Bottom = 0;

    //    myPane.XAxis.Title.Gap = 0;
    //    myPane.XAxis.Title.Text = "(h)";
    //    myPane.XAxis.Title.FontSpec.FontColor = Color.Red;
    //    myPane.XAxis.Title.FontSpec.Size = 24;

    //    myPane.YAxis.Title.Text = "(MW)";
    //    myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
    //    myPane.YAxis.Title.FontSpec.Size = 24;

    //    myPane.Legend.FontSpec.Size = 20f;
    //    myPane.Legend.Fill = new Fill(Color.Black);
    //    myPane.Legend.FontSpec.Fill = new Fill(Color.Black);
    //    myPane.Legend.FontSpec.FontColor = Color.Red;
    //    myPane.Legend.FontSpec.IsBold = true;
    //    //myPane.Legend.FontSpec.s
    //    //myPane.Legend.Gap = 1;

    //    PointPairList list1 = new PointPairList();
    //    PointPairList list2 = new PointPairList();

    //    int k = 0;
    //    for (int i = 0; i < 24; i++)
    //    {
    //        k = i;
    //        if (value[i] != null)
    //            list1.Add(k, double.Parse(value[i].ToString()));
    //        else
    //            list1.Add(k, 0);

    //        if (value2[i] != null)
    //            list2.Add(k, double.Parse(value2[i].ToString()));
    //        else
    //            list2.Add(k, 0);

    //    }

    //    // Generate a red curve with diamond
    //    //symbols, and "My Curve" in the legend
    //    //  LineItem myCurve3 = myPane.AddCurve("昨日负荷",
    //    //null, Color.Black, SymbolType.Diamond);

    //    LineItem myCurve = myPane.AddCurve("黄色#1机组",
    //         list1, Color.Orange, SymbolType.Diamond);

    //    LineItem myCurve2 = myPane.AddCurve("蓝色#2机组",
    //      list2, Color.Blue, SymbolType.Diamond);


    //    //LineItem myCurve = myPane.AddCurve("Smooth", dtTime, value, Color.Red, SymbolType.Diamond);

    //    //LineItem myCurve2 = myPane.AddCurve("tw", dtTime, value2, Color.Red, SymbolType.Diamond);

    //    //Make the curve smooth
    //    myCurve.Line.IsSmooth = true;
    //    myCurve2.Line.IsSmooth = true;


    //    // Set the XAxis to Text type
    //    //myPane.XAxis.Type = AxisType.LinearAsOrdinal;
    //    // Set the XAxis labels
    //    myPane.XAxis.Scale.TextLabels = dtTime;// labels;
    //    // Set the labels at an angle so they don't overlap
    //    //myPane.XAxis.Scale.FontSpec.Angle = 0;

    //    // Set the XAxis to Text type
    //    //myPane.XAxis.
    //    myPane.XAxis.Type = AxisType.Text; //AxisType.Text
    //    //myPane.XAxis.CrossAuto = true;
    //    myPane.XAxis.Scale.Min = 1;
    //    myPane.XAxis.Scale.Max = 24;
    //    myPane.XAxis.Scale.BaseTic = 0;

    //    // Set the XAxis labels
    //    //myPane.XAxis.Scale.TextLabels = lable;
    //    // Set the labels at an angle so they don't overlap
    //    //myPane.XAxis.ScaleFontSpec.Angle = 40;
    //    //myPane.XAxis.Scale.FontSpec.Angle = 45;

    //    myPane.XAxis.Scale.FontSpec.Size = 24;
    //    myPane.XAxis.Color = Color.Red;
    //    myPane.XAxis.MinorTic.Color = Color.Red;

    //    myPane.XAxis.Scale.FontSpec.FontColor = Color.Red;
    //    myPane.YAxis.Scale.FontSpec.Size = 24;
    //    myPane.YAxis.Color = Color.Red;
    //    myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;

    //    //myPane.Chart.Fill.Color = Color.Black;
    //    myPane.Chart.Fill = new Fill(Color.Black, Color.Black, Color.Black, 45.0F);
    //    //myPane.Chart.Border.IsAntiAlias = false;
    //    myPane.Chart.Border.Color = Color.Red;
    //    myPane.Y2Axis.IsVisible = false;
    //    //// title 设置

    //    //myPane.Title.IsVisible = true;
    //    //myPane.Title.Text = title; //"测点数据比较曲线图";

    //    //填充背景颜色
    //    // Fill the axis background with a color gradient
    //    myPane.Fill = new Fill(Color.Black, Color.Black, Color.Black, 45.0F);//Color.LightGoldenrodYellow

    //    // 在数据变化时绘制图形
    //    myPane.AxisChange(g);
    //}

    /// <summary>
    /// 生成固定个数颜色
    /// </summary>
    /// <param name="count">生成个数</param>
    /// <returns>泛型 Color</returns>
    private List<Color> GetColor(int count)
    {
        List<Color> Tcolors = new List<Color>();
        Random random = new Random();
        int i = 0;
        while (i < count)
        {
            int red = random.Next(255);
            int green = random.Next(255);
            int blue = random.Next(255);

            Color color = Color.FromArgb(red, green, blue);
            if (!Tcolors.Contains(color))
            {
                Tcolors.Add(color);
                i++;
            }
        }
        return Tcolors;
    }

    private double retDaysInYear(DateTime dt)
    {
        DateTime qsrq;
        DateTime jsrq;

        TimeSpan ts;

        qsrq = DateTime.Parse(dt.ToString("yyyy-01-01 0:00:00"));
        jsrq = DateTime.Parse(dt.AddYears(+1).ToString("yyyy-01-01 0:00:00"));

        ts = jsrq - qsrq;

        if (ts.Days > 0)
        {
            return ts.Days;
        }
        else { return 0; }
    }

    private double retDayYear(DateTime dt)
    {
        DateTime qsrq;
        DateTime jsrq;

        TimeSpan ts;

        qsrq = DateTime.Parse(dt.ToString("yyyy-01-01 0:00:00"));
        jsrq = DateTime.Parse(dt.ToString("yyyy-MM-dd 0:00:00"));

        ts = jsrq - qsrq;

        if (ts.Days > 0)
        {
            return ts.Days + 1;
        }
        else { return 0; }
    }
}
